$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'aic/membermachine/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true },
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '矿机编号', name: 'machineNo', index: 'machine_no', width: 80 }, 			
			{ label: '矿机名称', name: 'machineName', index: 'machine_name', width: 80 }, 			
			{ label: '矿机价格', name: 'price', index: 'price', width: 80 },
			{ label: '算力', name: 'hashSecond', index: 'hash_second', width: 80 }, 			
			{ label: '总天数', name: 'totalDays', index: 'total_days', width: 80 }, 			
			{ label: '已运行天数', name: 'runningDays', index: 'running_days', width: 80 }, 			
			{ label: '总收益', name: 'totalProfit', index: 'total_profit', width: 80 }, 			
			{ label: '每日收益', name: 'dayProfit', index: 'day_profit', width: 80 }, 			
			{ label: '累计收益', name: 'sumProfit', index: 'sum_profit', width: 80 }, 			
			{ label: '待领取收益', name: 'preProfit', index: 'pre_profit', width: 80 }, 			
			{ label: '矿机状态', name: 'status', index: 'status', width: 80,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-primary">运行中</span>';
					} else if (value == 1) {
						return '<span class="label label-success">结束</span>';
					} else if (value == 2) {
						return '<span class="label label-danger">暂停</span>';
					}
					return value;
				}
			},
			{ label: '赠送矿机', name: 'isReward', index: 'is_reward', width: 80,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-primary">否</span>';
					} else if (value == 1) {
						return '<span class="label label-success">是</span>';
					}
					return value;
				}
			},
			{ label: '购买时间', name: 'createTime', index: 'create_time', width: 80 }		
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{ mobile: null, status: ""},
		showList: true,
		title: null,
		memberMachine: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		changeStatus: function (type) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var url = "aic/membermachine/changeStatus";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    dataType: "json",
			    data: $.param({ids:ids,type:type},true),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "aic/membermachine/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "aic/membermachine/info/"+id, function(r){
                vm.memberMachine = r.memberMachine;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		}
	}
});